//
//  calenderDataBase.m
//  New
//
//  Created by dlios on 15-7-24.
//  Copyright (c) 2015年 yangTao. All rights reserved.
//

#import "calenderDataBase.h"

@implementation calenderDataBase

+(instancetype)shareCalenderDataBase{
    static calenderDataBase *calenderDB;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        calenderDB =[[calenderDataBase alloc]init];
    });

    return calenderDB;
    
}
static sqlite3 *db = nil;
//打开数据库
-(void)OpenDB{
    
    if (db != nil) {
        return;
    }
    NSArray *array  =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *filePath = [array lastObject];
    NSString *sqlitePath = [filePath stringByAppendingPathComponent:@"dataBase.sqlite"];
//    NSLog(@"***%@",sqlitePath);
    int result = sqlite3_open(sqlitePath.UTF8String, &db);
    if (result ==SQLITE_OK) {
//        NSLog(@"打开数据库成功");
    }else
    {
        
        NSLog(@"打开数据库失败");
    }
    
}
//创建列表
-(void)creatTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS calendTable(number INTEGER PRIMARY KEY AUTOINCREMENT,ID TEXT,localDateTime TEXT,importance TEXT,title TEXT,category_id TEXT,mark TEXT,forecast TEXT,actual TEXT,previous TEXT,country TEXT,currency TEXT,calendarType TEXT)";
    
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//        NSLog(@"创建表成功");
    }else
    {
        NSLog(@"创建表失败");
        
    }
}
-(void)insertNewModel:(CalenderModel *)calenderMod{
    
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO calendTable(ID,localDateTime,importance,title,category_id,mark,forecast,actual,previous,country,currency,calendarType)VALUES('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",calenderMod.ID,calenderMod.localDateTime,calenderMod.importance,calenderMod.title,calenderMod.category_id,calenderMod.mark,calenderMod.forecast,calenderMod.actual,calenderMod.previous,calenderMod.country,calenderMod.currency,calenderMod.calendarType];
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//        NSLog(@"添加成功");
    }else
    {
        
        NSLog(@"添加失败");
    }
    
    
}
-(NSMutableArray *)selectAllCalenderTable{
    
    NSString *sql = @"SELECT * FROM calendTable";
    
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result ==SQLITE_OK) {
//        NSLog(@"准备成功");
        while (sqlite3_step(stmt)==SQLITE_ROW) {
            const unsigned char *ID = sqlite3_column_text(stmt, 1);
            const unsigned char *localDateTime = sqlite3_column_text(stmt, 2);
            const unsigned char *importance = sqlite3_column_text(stmt, 3);
            const unsigned char *title = sqlite3_column_text(stmt, 4);
            const unsigned char *category_id = sqlite3_column_text(stmt, 5);
            const unsigned char *mark = sqlite3_column_text(stmt, 6);
            const unsigned char *forecast = sqlite3_column_text(stmt, 7);
            const unsigned char *actual = sqlite3_column_text(stmt, 8);
            const unsigned char *previous = sqlite3_column_text(stmt, 9);
            const unsigned char *country = sqlite3_column_text(stmt, 10);
            const unsigned char *currency = sqlite3_column_text(stmt, 11);
            const unsigned char *calendarType = sqlite3_column_text(stmt,12);
            
            CalenderModel *calendMod = [[CalenderModel alloc]init];
            calendMod.ID = [NSString stringWithUTF8String:(const char *)ID];
            calendMod.localDateTime = [NSString stringWithUTF8String:(const char *)localDateTime];
            calendMod.importance = [NSString stringWithUTF8String:(const char *)importance];
            calendMod.title = [NSString stringWithUTF8String:(const char *)title];
            calendMod.category_id = [NSString stringWithUTF8String:(const char *)category_id];
            calendMod.mark = [NSString stringWithUTF8String:(const char *)mark];
            calendMod.forecast = [NSString stringWithUTF8String:(const char *)forecast];
            calendMod.actual = [NSString stringWithUTF8String:(const char *)actual];
            calendMod.previous = [NSString stringWithUTF8String:(const char *)previous];
            calendMod.country = [NSString stringWithUTF8String:(const char *)country];
            calendMod.currency = [NSString stringWithUTF8String:(const char *)currency];
            calendMod.calendarType = [NSString stringWithUTF8String:(const char *)calendarType];

            [array addObject:calendMod];
        }
        
        
        return array;
    }else{
        NSLog(@"准备失败");
        return array;
        
    }
    
    
}
-(void)dropTable
{
    NSString *sql = @"DROP TABLE calendTable";
    int result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result ==SQLITE_OK) {
//        NSLog(@"删除成功");
    }else
    {
        
        NSLog(@"删除失败");
        
    }
    
    
}



@end
